#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e4 + 10;
int arr[N];
int brr[N];
int n, k;

int quick_sort(int l, int r, int k)
{
	if (l == r) {
		return arr[l];
	}
	int x = arr[(l + r )>> 1];
	int i = l - 1;
	int j = r + 1;
	while (i < j) {
		while (arr[++i] < x);
		while (arr[--j] > x);
		if (i < j) {
			swap(arr[i], arr[j]);
		}
	}
	int s1 = j - l + 1;
	if (s1 >= k ) {
		return quick_sort(l, j, k);
	}
	else {
		return quick_sort(j + 1, r, k - s1);
	}
}
int main()
{
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
		brr[i] = arr[i];
	}
	sort(brr, brr + n);
	for (int i = 1; i <= n; i++) {
		cout << brr[i] << " ";
	}
	cout << endl;
	int ret = quick_sort(1, n ,  k);
	cout << ret << endl;
	return 0;
}